import esbuild from "esbuild";
import path from "path";
import { fileURLToPath } from "url";
import { sassPlugin } from "esbuild-sass-plugin";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const entry = path.resolve(__dirname, "packages/index.js");
const outFile = path.resolve(__dirname, "packages/dist/index.js");

esbuild
  .build({
    entryPoints: [entry],
    bundle: true, // 打包所有依赖
    outfile: outFile,
    format: "esm", // 现代浏览器/打包器友好
    minify: true,
    sourcemap: false,
    target: ["esnext"],
    legalComments: "none",
    drop: ["console"], // 打包时删除所有console
    plugins: [sassPlugin()], // 新增：支持scss
  })
  .then(() => {
    console.log("已打包为单一 ESM 文件，所有依赖已包含。");
  })
  .catch((e) => {
    console.error("打包失败:", e);
    process.exit(1);
  });
